package cn.sykj.car.service.impl;

import cn.sykj.car.common.Const;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.entity.CarStore;
import cn.sykj.car.entity.ConfigBankAccount;
import cn.sykj.car.exception.CommonException;
import cn.sykj.car.exception.DictionaryDictException;
import cn.sykj.car.exception.ParameterInvalidException;
import cn.sykj.car.mapper.ConfigBankAccountMapper;
import cn.sykj.car.service.IConfigBankAccountService;
import cn.sykj.car.utils.DiffEntityUtils;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 银行账户管理 服务实现类
 * </p>
 *
 * @author xuming
 * @since 2021-06-24
 */
@Service
public class ConfigBankAccountServiceImpl extends ServiceImpl<ConfigBankAccountMapper, ConfigBankAccount> implements IConfigBankAccountService {

    /**
     * 添加银行账户信息
     *
     * @param configBankAccount
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO addConfigBankAccount(ConfigBankAccount configBankAccount) throws Exception {

        // 判断参数是否为空
        if (Utils.isEmpty(configBankAccount)) {
            throw new ParameterInvalidException();
        }

        // 判断开户行不能为空
        if (Utils.isEmpty(configBankAccount.getMainBankName())) {
            throw new CommonException("", "开户行不能为空");
        }

        // 判断支行不能为空
        if (Utils.isEmpty(configBankAccount.getBranchBankName())) {
            throw new CommonException("", "开户支行不能为空");
        }

        // 判断开户名不能为空
        if (Utils.isEmpty(configBankAccount.getUserName())) {
            throw new CommonException("", "开户名不能为空");
        }

        // 开户账号不能为空
        if (Utils.isEmpty(configBankAccount.getUserAccount())) {
            throw new CommonException("", "开户账号不能为空");
        }

        boolean ret = this.save(configBankAccount);

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("添加银行账户信息");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("添加银行账户信息，ID为：" + configBankAccount.getId() +
                ";" + "添加门店名称:" + configBankAccount.getMainBankName());
        return responseMessageVO;
    }

    /**
     * 删除银行账户信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO deleteConfigBankAccount(Integer id) throws Exception {

        //判断参数是否为空
        if (Utils.isEmpty(id)) {
            throw new ParameterInvalidException();
        }

        // 判断参数是否存在
        ConfigBankAccount configBankAccount = this.getById(id);
        if (Utils.isEmpty(configBankAccount)) {
            throw new CommonException(StatusCode.CONFIG_BANK_ACCOUNT_NOT_FOUND.getCode(),
                    StatusCode.CONFIG_BANK_ACCOUNT_NOT_FOUND.getMsg());
        }

        boolean ret = this.removeById(id);
        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("删除银行账户信息");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("删除银行账户信息，ID为:" + configBankAccount.getId() +
                ";" + "删除银行账户名称:" + configBankAccount.getMainBankName());
        return responseMessageVO;
    }

    /**
     * 修改银行账户信息
     *
     * @param configBankAccount
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ResponseMessageVO updateConfigBankAccount(ConfigBankAccount configBankAccount) throws Exception {

        // 验证参数是否为空
        if (Utils.isEmpty(configBankAccount) || Utils.isEmpty(configBankAccount.getId())) {
            throw new ParameterInvalidException();
        }

        // 判断原始记录是否存在
        ConfigBankAccount o_configBankAccount = this.getById(configBankAccount.getId());
        if (Utils.isEmpty(o_configBankAccount) || o_configBankAccount.getDelFlag().equals(Const.DEL_FLAG_TRUE)) {
            throw new DictionaryDictException("", "记录不存在");
        }

        // 把要更新的对象中的属性，覆盖到已有对象中
        boolean ret = baseMapper.updateConfigBankAccount(configBankAccount) > 0;

        // 定义修改字符串：
        String editMes = "";

        // 遍历修改的内容与原始内容相比较，如果有变更，则记录
        ConfigBankAccount afterConfigBankAccount = this.getById(configBankAccount.getId());
        Map<String, List<Object>> diffMap = DiffEntityUtils.getDifferent(o_configBankAccount, afterConfigBankAccount);
        if (Utils.notEmpty(diffMap) && diffMap.size() > 0) {
            for (String s : diffMap.keySet()) {
                editMes = editMes + ">" + s + "==>修改前【" + diffMap.get(s).get(0) + "】-修改后【" + diffMap.get(s).get(1) + "】;";
            }
        }

        ResponseMessageVO responseMessageVO = new ResponseMessageVO();
        responseMessageVO.setRetType("修改银行账户信息");
        responseMessageVO.setRetFlag(ret);
        responseMessageVO.setRetMsg("修改银行账户信息，ID为：" + afterConfigBankAccount.getId() + ";具体修改信息为：" + editMes);
        return responseMessageVO;
    }
}
